package com.arrogames.rgblabyrinth.model {
	import com.arrogames.rgblabyrinth.RGBLabyrinth;
	import com.arrogames.rgblabyrinth.controller.IRGBModel;
	import com.arrogames.rgblabyrinth.game.GameEngine;
	import com.arrogames.rgblabyrinth.model.storage.GameMetaData;
	import com.arrogames.rgblabyrinth.model.storage.RGBStorage;
	import com.arrogames.util.Observable;
	
	import org.as3commons.logging.api.ILogger;
	import org.as3commons.logging.api.getLogger;
	
	public class RGBModel extends Observable implements IRGBModel {
		private static const logger:ILogger = getLogger(RGBModel);

		private var _sfxEnabled:Boolean;
		private var _musicEnabled:Boolean;
		
		private var application:RGBLabyrinth;

		private var storage:RGBStorage;
		private var engine:GameEngine;

		public function RGBModel(engine:GameEngine) {
			super();
			this.engine = engine;
			
			_sfxEnabled = true;
			_musicEnabled = true;
		}
		
		public function initialize():void {
			storage = new RGBStorage();
			
			setChanged();
			notifyObservers(RGBModelEvent.INITIALIZED);
		}
		
		public function startNewGame(color:uint):void {
			setChanged();
			notifyObservers(RGBModelEvent.LOADING_LEVEL);
			var game:GameMetaData = new GameMetaData();
			game.baseColor = color;
			storage.addSaveGame(game);
			
			//gameLoader.loadNewLevel(game.level);
			setChanged();
			notifyObservers(RGBModelEvent.LEVEL_LOADED);
			
			engine.loadLevel();
			engine.start();
			
			logger.info("Created a new game");
		}
		
		public function continueGame(game:GameMetaData):void {
			setChanged();
			notifyObservers(RGBModelEvent.LOADING_LEVEL);
			
			//gameLoader.loadLevel(game.level, game.gameState);
			setChanged();
			notifyObservers(RGBModelEvent.LEVEL_LOADED);
			
			engine.loadLevel();
			engine.start();
			
			logger.info("Continued a game with level {0}, progress {1}%", [game.level, game.progress]);
		}
		
		public function removeGame(game:GameMetaData):void {
			storage.removeGame(game);
			setChanged();
			notifyObservers(RGBModelEvent.REMOVED_GAME);
			logger.info("Removed a game with level {0}, progress {1}%", [game.level, game.progress]);
		}
		
		public function stopCurrentGame():void {
			engine.stop();
			
			setChanged();
			notifyObservers(RGBModelEvent.STOPPED_GAME);
			logger.info("Stopped current game");
		}
		
		public function getSaveGames():Array {
			return storage.savegames;
		}
		
		public function get isMusicEnabled():Boolean { return _musicEnabled; }
		public function set isMusicEnabled(value:Boolean):void {
			_musicEnabled = value;
			setChanged();
			notifyObservers(RGBModelEvent.MUSIC_CHANGED);
			logger.info("Toggled music to {0}", [_musicEnabled ? "on" : "off"]);
		}
		
		public function get isSfxEnabled():Boolean { return _sfxEnabled; }
		public function set isSfxEnabled(value:Boolean):void {
			_sfxEnabled = value;
			setChanged();
			notifyObservers(RGBModelEvent.SFX_CHANGED);
			logger.info("Toggled sfx to {0}", [_sfxEnabled ? "on" : "off"]);
		}
		
		public function get isPaused():Boolean { return engine.isPaused; }
		public function set isPaused(value:Boolean):void {
			engine.isPaused = value;
		}
		
	}
}